<?php
namespace app\manage\controller;
use think\Db;

class RebateChannel extends Base
{
    public function index(){
        if(request()->isAjax()){
            $page = input('page');
            $limit = input('limit');
            $id = input('id');
            $gameId = input('game_id');
            $channelId = input('channel_id');
            $status = input('status');
            $roleId = input('role_id');
            $roleName = input('role_name');
            $date = explodeTime(input('date'));
            $adminId = input('admin_id');

            $where['apply_time'] = ['between', [$date['start_time'], $date['end_time']]];
            !empty($id) && $where['id'] = $id;
            !empty($gameId) && $where['game_id'] = $gameId;
            if(!empty($channelId)){
                if($channelId>5000){
                    $where['channel_id'] = $channelId;
                }else{
                    $where['cps_id'] = $channelId;
                }
            }
            if(!empty($status)||is_numeric($status)){
                $where['status'] = $status;
            }
            !empty($adminId) && $where['admin_id'] = $adminId;
            !empty($roleId) && $where['role_id'] = $roleId;
            !empty($roleName) && $where['role_name'] = ['like', "%{$roleName}%"];

            if( ! $count=model('ChannelRebate')->where($where)->count() ){
               $this->layTable();
            }

            $select = model('ChannelRebate')
                ->where($where)
                ->limit(($page-1)*$limit, $limit)
                ->order('apply_time desc')
                ->select();

            $data = [];
            $gameIdName = model('Game')->gameIdName();
            $channelIdName = model('Channel')->channelIdName();
            $channelRebateStatus = config('channel_rebate_status');
            $adminIdName = changeValKey('SystemAdmin', 'id', 'remark', 'roleid=4');

            foreach ($select as $k=>$v){
                $status = $v['status'];

                //如果未收录的大于一天改成未查看
                if($v['status']==1){
                    if( (time()-$v['receive_time']) > 86400 ){
                        Db::name('ChannelRebate')->where('id', $v['id'])->update(['status'=>0]);
                        $status = 0;
                    }
                }

                $data[] = [
                    'id' => $v['id'],
                    'cps_id' => !empty($v['cps_id']) ? $v['cps_id'] . '：' . $channelIdName[$v['cps_id']] : '',
                    'channel_id' => !empty($v['channel_id']) ? $v['channel_id'] . '：' .$channelIdName[$v['channel_id']] : '',
                    'game_id' => $v['game_id'] ? $v['game_id'] . '：' . $gameIdName[$v['game_id']] : $v['game_name'],
                    'server_name' => $v['server_name'],
                    'role_name' => $v['role_name'],
                    'role_id' => $v['role_id'],
                    'pay_money' => $v['pay_money'],
                    'pay_time' => date('Y-m-d', $v['pay_time']),
                    'pay_id' => $v['pay_id'],
                    'remark' => $v['remark'],
                    'message' => $v['message'],
                    'reason' => $v['reason'],
                    'apply_time' => date('Y-m-d H:i:s', $v['apply_time']),
                    'finish_time' => $v['finish_time'] ? date('Y-m-d H:i:s', $v['finish_time']) : '',
                    'status' => $channelRebateStatus[ $status ],
                    'admin_id' => isset($adminIdName[ $v['admin_id'] ]) ? $adminIdName[ $v['admin_id'] ] : ''
                ];
            }

            $this->layTable(0, '渠道返利列表', $data, $count);
        }else{
            $adminId = model('SystemAdmin')->where('roleid', 4)->select();
            return view('', ['date'=>date('Y-m-d - Y-m-d'), 'admin_id'=>$adminId]);
        }
    }

    //已收录
    public function receive(){
        $where['id'] = input('id');
        $where['status'] = 0;
        if( !model('ChannelRebate')->where($where)->count() ){
            $this->layJson(1, '该返利已被其他管理员收录，请右键刷新查看最新状态');
        }

        $editRs = model('ChannelRebate')->save([
            'status' => 1,
            'admin_id' => session('admin.id'),
            'receive_time' => time(),
        ], ['id' => input('id')]);
        $channelRebateStatus = config('channel_rebate_status');
        $editRs ? $this->layJson(0, '收录成功', $channelRebateStatus[1]) : $this->layJson(1, '收录失败，请重试');
    }

    //已发放
    public function finish(){
        $where['admin_id'] = session('admin.id');
        $where['id'] = input('id');
        $where['status'] = 1;
        if( !model('ChannelRebate')->where($where)->count() ){
            $this->layJson(1, '该返利已被其他管理员收录，你不能点击发放操作');
        }

        $editRs = model('ChannelRebate')->save([
            'status' => 2,
            'finish_time' => time(),
            'message' => input('message')
        ], ['id' => input('id')]);
        $channelRebateStatus = config('channel_rebate_status');
        $editRs ? $this->layJson(0, '发放成功', ['status'=>$channelRebateStatus[2], 'message'=>input('message')]) : $this->layJson(1, '发放失败，请重试');
    }

    //被驳回
    public function reject(){
        $where['admin_id'] = session('admin.id');
        $where['id'] = input('id');
        $where['status'] = 0;
        if( !model('ChannelRebate')->where($where)->whereOr(['status'=>1])->count() ){
            $this->layJson(1, '该返利已被其他管理员收录，你不能点击驳回操作');
        }

        $editRs = model('ChannelRebate')->save([
            'admin_id' => session('admin.id'),
            'status' => 3,
            'reject_time' => time(),
            'reason' => input('reason')
        ], ['id' => input('id')]);

        $channelRebateStatus = config('channel_rebate_status');
        $editRs ? $this->layJson(0, '驳回成功', ['status'=>$channelRebateStatus[3], 'reason'=>input('reason')]) : $this->layJson(1, '驳回失败，请重试');
    }
}